<?php
/**
 * Copyright (c) 2018-2019.
 *  This file is part of the moonpie production
 *  (c) johnzhang <875010341@qq.com>
 *  This source file is subject to the MIT license that is bundled
 *  with this source code in the file LICENSE.
 */

use think\migration\db\Column;
use think\migration\Migrator;

class AddWechatTemplateSystem extends Migrator
{
    protected $tableWechatTemplate = 'wechat_template_config';
    protected $tableWechatUser = 'wechat_account_user';
    protected $tableWechatMenu = 'wechat_account_menu';
    protected $tableWechatTag = 'wechat_account_user_tag';

    public function up()
    {
        $this->createWechatTemplateConfig();
        $this->createWechatUser();
        $this->createWechatUserTag();
        $this->createWechatMenu();
    }

    public function down()
    {
        $this->dropWechatMenu();
        $this->dropWechatUserTag();
        $this->dropWechatUser();
        $this->dropWechatTemplateConfig();
    }

    protected function dropWechatMenu()
    {
        if ($this->hasTable($this->tableWechatMenu)) {
            $this->dropTable($this->tableWechatMenu);
        }
    }

    protected function dropWechatUserTag()
    {
        if ($this->hasTable($this->tableWechatTag)) {
            $this->dropTable($this->tableWechatTag);
        }
    }

    protected function createWechatUserTag()
    {
        if (!$this->hasTable($this->tableWechatTag)) {
            $table = $this->table($this->tableWechatTag, ['engine' => 'InnoDB', 'comment' => '公众号用户标签管理']);
            $table
                ->addColumn(Column::integer('wxuser_id')->setComment('关联的微信用户ID'))
                ->addColumn(Column::integer('tag_id')->setComment('关联的微信标签ID'))
                ->addColumn(Column::integer('create_at')->setComment('记录添加时间'))
                ->addColumn(Column::integer('update_at')->setComment('记录修改时间'))
                ->addIndex(['wxuser_id', 'tag_id'], ['name' => 'unique_wxuser_tag', 'unique' => true])
                ->addIndex('tag_id', ['name' => 'index_tag_user'])
                ->create();
        }
    }

    protected function createWechatMenu()
    {
        if (!$this->hasTable($this->tableWechatMenu)) {
            $table = $this->table($this->tableWechatMenu, ['engine' => 'InnoDB', 'comment' => '公众号自定义菜单']);
            $table
                ->addColumn(Column::integer('account_id')->setComment('关联的公众号信息'))
                ->addColumn(Column::string('menu_id', 30)->setComment('获取到的微信菜单ID'))
                ->addColumn(Column::string('menu_type', 20)->setComment('定义的菜单类型'))
                ->addColumn(Column::text('menu_data')->setComment('设置的微信菜单数据'))
                ->addColumn(Column::text('menu_backup')->setComment('备份的上次微信菜单数据'))
                ->addColumn(Column::integer('create_at')->setComment('记录添加时间'))
                ->addColumn(Column::integer('update_at')->setComment('记录修改时间'))
                ->addIndex(['account_id', 'menu_type'], ['name' => 'unique_menu_type', 'unique' => true])
                ->create();
        }
    }

    protected function dropWechatUser()
    {
        if ($this->hasTable($this->tableWechatUser)) {
            $this->dropTable($this->tableWechatUser);
        }
    }

    protected function createWechatUser()
    {
        if (!$this->hasTable($this->tableWechatUser)) {
            $table = $this->table($this->tableWechatUser, ['engine' => 'InnoDB', 'comment' => '公众号用户信息']);
            $table
                ->addColumn(Column::integer('account_id')->setComment('关联的公众号信息'))
                ->addColumn(Column::char('openid', 28)->setComment('用户openid'))
                ->addColumn(Column::string('unionid', 40)->setComment('用户unionid')->setNullable())
                ->addColumn(Column::string('nickname', 100)->setComment('用户微信昵称'))
                ->addColumn(Column::string('avatar')->setComment('用户微信头像'))
                ->addColumn(Column::boolean('sex')->setComment('用户性别'))
                ->addColumn(Column::string('language', 20)->setComment('用户语言'))
                ->addColumn(Column::string('country', 40)->setComment('用户所在国家'))
                ->addColumn(Column::string('province', 40)->setComment('用户所在省份'))
                ->addColumn(Column::string('city', 40)->setComment('用户所在城市'))
                ->addColumn(Column::boolean('subscribe')->setComment('用户是否关注公众号'))
                ->addColumn(Column::string('subscribe_scene')->setComment('用户关注公众号场景值'))
                ->addColumn(Column::integer('subscribe_at')->setComment('用户关注时间'))
                ->addColumn(Column::integer('create_at')->setComment('记录添加时间'))
                ->addColumn(Column::integer('update_at')->setComment('记录修改时间'))
                ->addIndex(['account_id', 'openid'], ['name' => 'unique_openid', 'unique' => true])
                ->addIndex(['account_id', 'unionid'], ['name' => 'unique_unionid', 'unique' => true])
                ->create();
        }
    }

    protected function createWechatTemplateConfig()
    {
        if (!$this->hasTable($this->tableWechatTemplate)) {
            $table = $this->table($this->tableWechatTemplate, ['engine' => 'InnoDB', 'comment' => '使用的公众号模板消息配置信息']);
            $table
                ->addColumn(Column::integer('account_id')->setComment('关联的公众号信息'))
                ->addColumn(Column::string('title', 40)->setComment('模板发送标题'))
                ->addColumn(Column::string('description', 100)->setComment('模板发送描述'))
                ->addColumn(Column::string('code', 20)->setComment('配置唯一标识')->setUnique())
                ->addColumn(Column::string('template_id', 60)->setComment('公众号模板ID'))
                ->addColumn(Column::string('template_parser', 100)->setComment('使用的模板解析对象信息'))
                ->addColumn(Column::text('template_data')->setComment('设置的模板变量'))
                ->addColumn(Column::boolean('status')->setComment('公众号状态，0不可用1可用'))
                ->addColumn(Column::integer('create_at')->setComment('记录添加时间'))
                ->addColumn(Column::integer('update_at')->setComment('记录修改时间'))
                ->addIndex(['account_id', 'status'], ['name' => 'index_account_status'])
                ->create();
        }
    }

    protected function dropWechatTemplateConfig()
    {
        if ($this->hasTable($this->tableWechatTemplate)) {
            $this->dropTable($this->tableWechatTemplate);
        }
    }
}
